;(function(){
	/**<pre>
	 * edi的菜单组件，
	 * config={
	 *	 	minWidth:160,
			menu:[{
					type:'map',	//菜单类型
					items:[{	//菜单类型对应的菜单项
						name:'',
						code:''
					}]
				}]
			onclickItem:function(type,code,commandData){}	
			};
	 * <ul class="dropdown-menu EdiMenu" id="${id}" style="min-width:${minWidth}px;" >
		<li class="${type}" code="${items[].code}"><a href="javascript:void(0)">${items[].name}</a></li>
		<li class="${type}" code="${items[].code}"><a href="javascript:void(0)">${items[].name}</a></li>
	</ul></pre>*/
	var EdiMenu=function(setting){
		this.id="edi-resource-menu"+(EdiMenu.instanceCount++);
		this.setting={
			minWidth:120
		};
		$.extend(true,this.setting,setting);
		this.commandData={};//出发菜单显示的上下文数据
		this.curX = 0;
		this.curY = 0;
		this.init();
	};
	EdiMenu.prototype={
		render:function(){
			var html=[];
			html.push('<ul class="dropdown-menu EdiMenu" id="'+this.id+'" style="min-width:'+this.setting.minWidth+'px;" >');
			this.setting.menu&&$.each(this.setting.menu,function(){
				var type=this.type;
				this.items&&$.each(this.items,function(){
					html.push('<li class="'+type+'" code="'+this.code+'"><a href="javascript:void(0)">'+this.name+'</a></li>');
				});
			});
			html.push('</ul>');
			$("body").append(html.join(''));
			this.$id=$('#'+this.id);
		},
		bind:function(){
			var that=this;
			this.$id.on('click','li',function(){
				var $this=$(this);
				if(that.setting.onclickItem)
				{
					that.setting.onclickItem($this.attr('class'),$this.attr('code'),that.commandData);
					that.hideRMenu();
				}
			});
		},
		//销毁前的处理函数
		destructor:function(){
			this.$id.off();
			this.$id.remove();
		},
		filterMenu:function(type){
			this.type=type;
			if(!type)
				return;
			
			var types=type.split(','),
				that=this;
			$.each(types,function(){
				$('#'+that.id+' li.'+this).css("display","block");
			});
		},
		showMenu:function(type,commandData){
			var that=this;
			$('#'+this.id+" li").css("display","none");
			this.filterMenu(type);
			this.commandData=commandData;
			this.setting.menu&&$.each(this.setting.menu,function(){
				var type=this.type;
				this.items&&$.each(this.items,function(){
					if(this.isShow&&!this.isShow())
						$('#'+that.id+' li[code='+this.code+']').css("display","none");
				});
			});
			var scrollTop = document.documentElement.scrollTop || document.body.scrollTop;
			var scrollLeft = document.documentElement.scrollLeft || document.body.scrollLeft;
			this.curX = event.clientX+scrollLeft, this.curY = event.clientY+scrollTop;
			this.$id.css({
				"top" : this.curY + "px",
				"left" : this.curX + "px",
				"display" : "block"
			});
			var that=this;
			$("body").bind("mousedown",function(event){ that.onBodyMouseDown(event);});
			return false;
		},
		hideRMenu : function() {
			this.$id.css({
				"display" : "none"
			});
			$("body").unbind("mousedown", this.onBodyMouseDown);
		},
		onBodyMouseDown : function(event) {
			if (!(event.target.id == this.divId || $(event.target).parents('#'+this.id).length > 0)) {
				this.$id.css({
					"display" : "none"
				});
			}
		},
		init:function(){
			this.render();
			this.bind();
		}
	};
	EdiMenu.instanceCount=0;
	window["EdiMenu"]=EdiMenu;
})();
